package com.yst.metho.web.application.projectversion.views;

import java.util.Collection;

import javax.ejb.CreateException;
import javax.ejb.FinderException;
import javax.naming.NamingException;

import nextapp.echo2.app.Column;
import nextapp.echo2.app.Extent;
import nextapp.echo2.app.Grid;
import nextapp.echo2.app.Insets;
import nextapp.echo2.app.Label;
import nextapp.echo2.app.SelectField;
import nextapp.echo2.app.TextField;

import com.yst.metho.web.application.components.NamedObjectListRenderer;
import com.yst.metho.web.application.projectversion.renderer.ActivityListCellRenderer;
import com.yst.metho.web.application.utils.GenericModel;
import com.yst.suivi.data.activities.Activity;
import com.yst.suivi.data.folders.Folder;
import com.yst.suivi.data.project.versions.ProjectVersion;
import com.yst.suivi.data.project.versions.lot.Lot;
import com.yst.suivi.data.team.TeamRole;
import com.yst.suivi.status.StatusConstants;

public class ActivityPane extends Column
{
	private TextField mNom;

	private TextField mDuree;

	private SelectField mClassification;

	private SelectField mTeam;

	private Lot mLot;

	private String mId = null;

	private ProjectVersion mVersion;

	public ActivityPane(ProjectVersion version, Activity act, Collection folders, Collection teamRoles, Lot lot) throws FinderException, NamingException, CreateException
	{
		super();

		mVersion = version;
		mLot = lot;

		setCellSpacing(new Extent(10));
		setInsets(new Insets(10));
		Grid headerGrid = new Grid();
		headerGrid.setInsets(new Insets(0, 2));
		add(headerGrid);

		Label label;
		label = new Label("Nom");
		headerGrid.add(label);
		mNom = new TextField();
		mNom.setStyleName("TextField");
		headerGrid.add(mNom);

		label = new Label("Duree");
		headerGrid.add(label);
		mDuree = new TextField();
		mDuree.setStyleName("TextField");
		headerGrid.add(mDuree);

		label = new Label("Classification");
		headerGrid.add(label);
		mClassification = new SelectField();
		mClassification.setModel(new GenericModel(folders));
		mClassification.setStyleName("TextField");
		mClassification.setCellRenderer(new ActivityListCellRenderer());
		headerGrid.add(mClassification);

		label = new Label("Role");
		headerGrid.add(label);
		mTeam = new SelectField();
		mTeam.setModel(new GenericModel(teamRoles));
		mTeam.setStyleName("TextField");
		mTeam.setCellRenderer(new NamedObjectListRenderer());
		headerGrid.add(mTeam);

		label = new Label("Lot");
		headerGrid.add(label);
		label = new Label(lot.getNom());
		headerGrid.add(label);

		if (null != act)
		{
			mId = act.getId();
			mNom.setText(act.getNom());
			mDuree.setText(act.getDuree().toString());
			mClassification.setSelectedItem(act.getFolder());
			mTeam.setSelectedItem(act.getTeamRole());
			if (StatusConstants.CLOSE == act.getStatus())
			{
				mNom.setEnabled(false);
				mDuree.setEnabled(false);
				mClassification.setEnabled(false);
				mTeam.setEnabled(false);
			}
		}
	}

	public ActivityPane(ProjectVersion version, Collection folders, Collection teamRoles, Lot lot) throws FinderException, NamingException, CreateException
	{
		this(version, null, folders, teamRoles, lot);
	}

	/**
	 * @return the id
	 */
	public String getId()
	{
		return mId;
	}

	/**
	 * @return the login
	 */
	public String getNom()
	{
		return mNom.getText();
	}

	/**
	 * @return the classification
	 */
	public Folder getClassification()
	{
		return (Folder) mClassification.getSelectedItem();
	}

	/**
	 * @return the classification
	 */
	public TeamRole getTeamRole()
	{
		return (TeamRole) mTeam.getSelectedItem();
	}

	/**
	 * @return the duree
	 */
	public float getDuree()
	{
		return Float.parseFloat(mDuree.getText());
	}

	/**
	 * @return the version
	 */
	public ProjectVersion getVersion()
	{
		return mVersion;
	}

	/**
	 * @return the lot
	 */
	public Lot getLot()
	{
		return mLot;
	}

}
